import { SpriteFrame } from "cc"
import TaskApi from "../api/TaskApi"
import ResManager from "../common/ResManager"
import { UserInfo } from "../common/UserInfo"
import { TaskConfig, TaskStaticList, em_Condition_Name, } from "../settings/const"
import { Bonus } from "./Bonus"
import { RombawIng } from "./RombawIng"

/**任务类型 */
export enum TaskType {
    /**主线任务 */
    Main = 1,
    /**日常任务 */
    Daily = 2,
    /**其它任务 */
    Other = 3
}
/**任务状态 */
export enum TaskStatus {
    /**未完成 */
    NoComplete = 0,
    /**已完成 */
    Complete = 1,
    /**已领奖 */
    Received = 2
}

/**
 * 任务类
 */
export class Task {
    /**任务ID */
    id: number
    /**任务名称 */
    name: string
    /**任务条件 */
    condition: string
    /**任务奖励 */
    bonus: string
    /**前置任务ID */
    front: number
    /**任务层级 */
    layer: number
    /** */
    race: number
    /**任务状态 */
    status: TaskStatus = TaskStatus.NoComplete
    /**任务进度 */
    value: { type: number, value: number, threshold }[]

    type: TaskType


    switch: number


    /**
     * 根据任务ID 创建任务对象
     * @param id 
     */
    static createById(id: number): Task {

        let obj = TaskConfig[id]
        if (!obj) {
            return null
        }

        return this.createByObj(obj)
    }

    /**
     * 根据数据对象 创建任务对象
     * @returns 
     */
    static createByObj(obj: any): Task {

        let t = new Task()
        t.id = parseInt(obj.id)
        t.name = obj.name
        t.condition = obj.condition
        t.bonus = obj.bonus
        t.front = parseInt(obj.front)
        t.layer = parseInt(obj.layer)
        t.race = obj.race
        return t
    }

    /**
     * 根据接口数据，创建任务列表
     * @param data 
     * @returns 
     */
    static createByList(data: any[]): Task[] {
        let list: Task[] = []
        for (let item of data) {
            let task = this.createById(item.id)
            if (!task) {

                continue
            }
            task.status = item.status || 0
            task.value = item.value
            task.type = Math.floor(task.id / 1000)

            list.push(task)
        }
        return list
    }

    /**缓存所有的任务 */
    static allTaskList:Task[] = []
    /**
     * 根据任务类型，获取任务列表
     * @param type 
     * @returns 
     */
    static async getTaskListByType(type: TaskType): Promise<Task[]> {
        if(this.allTaskList.length == 0) {
            let res = await TaskApi.list({
                status: -1
            })
            
            for (let it of res.data.list) {
                for (let t of TaskStaticList) {
                    if (it.id == t.id) {
                        //用线上数据填充本地数据
                        t.status = it.status || 0;
                        t.value = it.value || 0;
                        t.switch = it.switch || 0;
                    }
                }
            }
            this.allTaskList = Task.createByList(TaskStaticList)
            console.log('任务数据：',this.allTaskList)
        }
        
        

        let list = this.allTaskList.filter((item) => {
            return item.type == type && item.status != TaskStatus.Received
        })

        list.sort((a, b) => {
            return b.status - a.status
        })

        return list
    }



    /**任务要求 */
    get conditionStr(): string {
        let desc = "";

        let bs = this.condition.split(";");
        for (let b of bs) {
            if (!!desc) {
                desc += " | ";
            }

            let ls = b.split(",");
            desc += em_Condition_Name[parseInt(ls[0])] + ": " + ls[1];
        }

        return desc;

    }

    /**任务类型 */
    get typeName(): string {

        switch (this.type) {
            case TaskType.Main: return '主线任务';
            case TaskType.Daily: return '日常任务';
            case TaskType.Other: return '循环任务';
            default: return '未知任务';
        }
    }

    /**任务状态及进度 */
    get statusStr(): string {
        switch (this.status) {
            case TaskStatus.Complete:
                return "待领奖";
            case TaskStatus.Received:
                return "已领奖";
            case TaskStatus.NoComplete:

                if(!this.value){
                    return ''
                }
                return `${this.value[0].value}/${this.value[0].threshold}`


                let str = ''
                if (this.value) {
                    for (let item of this.value) {
                        str += `${em_Condition_Name[item.type]}:${item.value}/${item.threshold};`

                    }
                }
                return str

        }
    }

    /**任务奖励 */
    get bonusStr(): string {

        return Bonus.bonusArrayDesc(this.bonus)
    }

    async getTaskImg(): Promise<SpriteFrame> {
        let img = this.condition.split(',')[0]
        return ResManager.instance.loadAssetByUrl<SpriteFrame>(`taskImg/${img}/spriteFrame`)
    }

}